package com.nju.bean.newsbangserver;

import java.sql.SQLException;

import com.nju.bean.cloud.Dot;
import com.nju.bean.newsbang.data.Fragment;
import com.nju.bean.newsbang.data.Topic;

public class FragmentManager {
	
	public static final int LIKE = 0;
	public static final int DISLIKE = 1;
	
	public boolean light(String id, int type) throws SQLException{
		Fragment fra = searchFragment(id);
		fra.light(type);
		if(type==LIKE)
			return PointManager.likeed(fra.getAuthorName(),fra.getNumOfLiking());
		else if(type==DISLIKE)
			return PointManager.dislikeed(fra.getAuthorName(), fra.getNumOfDisliking());
		else
			return false;
	}
	
	public boolean share (String id) throws SQLException{
		Fragment fra=this.searchFragment(id);
		fra.share();
		return PointManager.shared(id, fra.getNumOfSharing());
	}
	private Fragment searchFragment(String id){
		String temp=id.substring(0, 8);
		Fragment result = null;
		Topic topic = TopicManager.searchTopicById(temp);
		for(Dot dot:topic.getDotList()){
			Fragment fra =(Fragment)dot;
			if(fra.getId().equals(id)){
				result = fra;
				break;
			}				
		}
		return result;
	}
	
	public static void main(String arges[]){
		System.out.println("1234567890".substring(0, 8));
	}
}
